// cookie操作;
function getCookie(name){
    // 分号后面有空格,分号后面有空格,分号后面有空格
    var cookie_array = document.cookie.split("; ");
    // console.log(cookie_array)
    for(var i = 0 ; i < cookie_array.length ; i ++){
          // 取出每一条cookie
          var cookie_item = cookie_array[i].split("=");
          // 每一套cookie分成key和value;
          // 根据key 去获取value值;
          if(cookie_item[0] === name){
                return decodeURIComponent(cookie_item[1]);
          }
    }
}
function setCookie(name , value , expires , path , domain){
    var cookie_str = "";
    cookie_str += name + "=" + value;
    // 表示不为空
    if(expires && typeof expires === "number"){
          // 获取一个相应日期的日期对象;
          var d = new Date();
          d.setDate(d.getDate() + expires);
          // 拼接对应的字符串;
          cookie_str += ";expires=" + d;
    }
    // str = "key=value;expires="日期
    if(path && typeof path === "string"){
          // 拼接对应的字符串;
          cookie_str += ";path=" + path;
    }
    if(domain && typeof domain === "string"){
          // 拼接对应的字符串;
          cookie_str += ";domain=" + domain;
    }
    // console.log(cookie_str);
    document.cookie = cookie_str;
}
function removeCookie( name  , path){
    setCookie(name , null , -1 ,  path )
}
//cookie免登录功能
function viod_login(){
    var cookie_str = getCookie("USRID");
    var nav_list = document.querySelectorAll(".nav_list .first");
    var user_name = document.getElementById("user_name");
    try{
        // 因为在这里存在报错的可能,所以尽量把这段代码放在try catch里面;
        var cookie_array = JSON.parse(cookie_str);
        nav_list[1].style.display = "block";
        nav_list[0].style.display = "none";
        user_name.innerHTML = cookie_array.username;
    }catch(e){
        // 一旦 catch 则表示登陆状态不正确;
        nav_list[0].style.display = "block";
   }
  
   // 退出登陆;
    var signout = document.getElementById("signout");
    signout.onclick = function(){
          removeCookie("USRID","/");
          location.reload();
    };
}
viod_login();